package com.myidis.servlet;

import com.auxiliary.CICalculator;
import com.auxiliary.SeasonAdjuster;
import com.auxiliary.TimeSequence;
import com.myidis.entity.*;
import com.myidis.mapper.CIDIIndexManagementMapper;
import com.myidis.mapper.CIResultMapper;
import com.myidis.mapper.QuotaDataMapper;
import com.myidis.request.DICalculatorReq;
import com.myidis.request.SearchReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CICalculateServlet {
    @Autowired
    private QuotaDataMapper quotaDataMapper;
    @Autowired
    private CIDIIndexManagementMapper cidiIndexManagementMapper;
    @Autowired
    private CIResultMapper ciResultMapper;

    public ArrayList<IndexResultList> all() {
        return ciResultMapper.list();
    }

    public pictureReturn getData(SearchReq sea) {
        ArrayList<FullCIResult> a = ciResultMapper.getData(sea);

        pictureReturn b = new pictureReturn();
        ArrayList<String> dates = new ArrayList<>();
        ArrayList<Double> lead = new ArrayList<>();
        ArrayList<Double> coin = new ArrayList<>();
        ArrayList<Double> lag = new ArrayList<>();
        ArrayList<Double> benchmark = new ArrayList<>();
        ArrayList<String> lineName = new ArrayList<>();
        lineName.add("合成指数");
        lineName.add("先行合成指数");
        lineName.add("一致合成指数");
        lineName.add("滞后合成指数");
        for(int i = 0; i < a.size(); i++){
            dates.add(a.get(i).getTime());
            lead.add(a.get(i).getLeadCI());
            coin.add(a.get(i).getCoinCI());
            lag.add(a.get(i).getLagCI());
            benchmark.add(a.get(i).getBenchmarkCI());
        }
        b.setLead(lead);
        b.setCoin(coin);
        b.setLag(lag);
        b.setBenchmark(benchmark);
        b.setDates(dates);
        b.setLineName(lineName);
        return b;
    }

    public IndexResultList getInfo(int sea) {
        return ciResultMapper.getInfo(sea);
    }

    public ArrayList<CIResult> calculate(DICalculatorReq sea) {


        //先行指标组合时间序列的获取
        //从传回的json中拿到指标名称，查找对应的指标
        ArrayList<TimeSequence> LeadQuotaSet = new ArrayList<>();
        ArrayList<AnalysisIndex> LeadDataSet = cidiIndexManagementMapper.getByUniformity(sea.getCombination(), "先行");
        //通过查到的指标，以其名称取到具体数据并生成时间序列
        TimeSequence LeadTs = new TimeSequence();
        for (AnalysisIndex a: LeadDataSet){
            LeadTs.Rebuild(quotaDataMapper.getByQuota(a.getIndex(),sea.getStartTime(), sea.getEndTime()));
            SeasonAdjuster tc = new SeasonAdjuster(LeadTs,1,1);
            LeadTs.setValueList(tc.getTCList());
            LeadQuotaSet.add(LeadTs);
        }

        //一致指标组合时间序列的获取
        ArrayList<TimeSequence> CoinQuotaSet = new ArrayList<>();
        ArrayList<AnalysisIndex> CoinDataSet = cidiIndexManagementMapper.getByUniformity(sea.getCombination(), "一致");
        TimeSequence CoinTs = new TimeSequence();
        for (AnalysisIndex a: CoinDataSet) {
            CoinTs.Rebuild(quotaDataMapper.getByQuota(2,sea.getStartTime(), sea.getEndTime()));
            SeasonAdjuster tc = new SeasonAdjuster(CoinTs,1,1);
            CoinTs.setValueList(tc.getTCList());
            CoinQuotaSet.add(CoinTs);

        }



        //滞后指标组合时间序列的获取
        ArrayList<TimeSequence> LagQuotaSet = new ArrayList<>();
        ArrayList<AnalysisIndex> LagDataSet = cidiIndexManagementMapper.getByUniformity(sea.getCombination(), "滞后");
        TimeSequence LagTs = new TimeSequence();
        for (AnalysisIndex a: LagDataSet) {
            LagTs.Rebuild(quotaDataMapper.getByQuota(a.getIndex(), sea.getStartTime(), sea.getEndTime()));
            SeasonAdjuster tc = new SeasonAdjuster(LagTs,1,1);
            LagTs.setValueList(tc.getTCList());
            LagQuotaSet.add(LagTs);
        }

        //权值列表
        ArrayList<ArrayList<Double>> weightList = new ArrayList<ArrayList<Double>>();
        ArrayList<Double> leadWeight=new ArrayList<Double>();
        for (int i = 0; i < LeadQuotaSet.size(); i++)
            leadWeight.add(i+1.0);
        weightList.add(leadWeight);
        ArrayList<Double> coinWeight = new ArrayList<Double>();
        for (int i = 0; i < CoinQuotaSet.size(); i++)
            coinWeight.add(i+1.0);
        weightList.add(coinWeight);
        ArrayList<Double> lagWeight = new ArrayList<Double>();
        for (int i = 0; i < LagQuotaSet.size(); i++)
            lagWeight.add(i+1.0);
        weightList.add(lagWeight);

        //计算合成指数
        CIResult QuotaDIResult = new CICalculator(LeadQuotaSet,CoinQuotaSet,LagQuotaSet,weightList,sea.getSpringDay()).CalculateCI();


        //基准指数计算
//        ArrayList<TimeSequence> benchmarkSet = new ArrayList<>();
//        benchmarkSet.add(new TimeSequence(quotaDataMapper.getByOne(sea.getBenchmark())));
        SeasonAdjuster tc = new SeasonAdjuster(new TimeSequence(quotaDataMapper.getByOne(sea.getBenchmark())),1,1);

        CIResult finalResult = new CIResult(sea.getSubject(), LeadTs.getDateList(),QuotaDIResult, sea.getSpringDay(), tc.getTCList());

        ArrayList<CIResult> results = new ArrayList<>();
        results.add(finalResult);

        //把计算结果注入数据库


        String subject = finalResult.getSubject();
        String startTime = sea.getStartTime();
        String endTime = sea.getEndTime();
        String springDay = sea.getSpringDay();
        String combination = sea.getCombination();
        String calculator = sea.getCalculator();
        String calTime = sea.getCalTime();
        System.out.println(subject+" "+combination);
        ciResultMapper.insertList(new IndexResultList(0,subject,springDay,calTime,combination,calculator,startTime,endTime));


        for(int i = 0; i < finalResult.getCount(); i++){
            String time = finalResult.getDates().get(i);
            double LeadCI = finalResult.leadIndex.get(i);
            double CoinCI = finalResult.coinIndex.get(i);
            double LagCI = finalResult.lagIndex.get(i);
            double benchmarkCI = tc.getTCList().get(i);
            FullCIResult DataSql = new FullCIResult();
            DataSql.rebuild(0,subject,time,LeadCI,
                    CoinCI,LagCI,benchmarkCI);

            ciResultMapper.insertDetail(DataSql);
        }

        return results;
    }

    public int delect(int del) {
        ciResultMapper.deleteData(del);
        return ciResultMapper.delete(del);
    }



}
